package com.handinfo.android.download;

import java.io.File;

import com.handinfo.android.DWGameManager;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Update
{
	private UpdateManager updateMan;
	private ProgressDialog updateProgressDialog;

	public Context context;


	public Update(Context context, String url)
	{

		this.context = context;

		updateMan = new UpdateManager(context, appUpdateCb, url,DWGameManager.s_apk_dir + File.separator,false,"xycs_lite.apk");
		updateMan.checkUpdate();
	}
	public Update(Context context, String url,String savefolder,String name)
	{

		this.context = context;

		updateMan = new UpdateManager(context, appUpdateCb, url,savefolder,true,name);
		updateMan.checkUpdate();
	}

	// 回调控件
	UpdateManager.UpdateCallback appUpdateCb = new UpdateManager.UpdateCallback()
	{

		public void downloadProgressChanged(int progress)
		{
			if (updateProgressDialog != null
					&& updateProgressDialog.isShowing()) {
				updateProgressDialog.setProgress(progress);
			}

		}

		public void downloadCompleted(Boolean sucess, CharSequence errorMsg)
		{
			if (updateProgressDialog != null
					&& updateProgressDialog.isShowing()) {
				updateProgressDialog.dismiss();
			}
			if (sucess) {
				
				updateMan.update();
			}
			else {
				DialogHelper.Confirm(context,
						"错误",
						"下载更新失败，是否重试？",

						"重新下载",
						new DialogInterface.OnClickListener()

						{

							public void onClick(DialogInterface dialog,
									int which)
							{
								updateMan.downloadPackage();

							}
						}, "以后再说", null);
			}
		}

		public void downloadCanceled()
		{
			// TODO Auto-generated method stub
//				DWGameManager.getInstance().getGameHandler().setRunning(false);
//				DWGameManager.getInstance().setRunning(false);

		}

		public void checkUpdateCompleted(Boolean hasUpdate,
				CharSequence updateInfo)
		{
			if (hasUpdate) {
				DialogHelper.Confirm(context,
						"更新客户端",
						"有客户端更新是否更新？",
						//							+updateInfo+
//							context.getText(R.string.dialog_update_msg2).toString(),
						"确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int which)
							{
								updateProgressDialog = new ProgressDialog(context);
								updateProgressDialog.setMessage("正在下载更新。。。");
								updateProgressDialog.setIndeterminate(false);
								updateProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
								updateProgressDialog.setMax(100);
								updateProgressDialog.setProgress(0);
								updateProgressDialog.show();

								updateMan.downloadPackage();
							}
						}, "取消", null);
			}

		}
	};
}
